import { createRouter, createWebHashHistory } from 'vue-router'

const Login = () => import('../components/Login')
const Home = () => import('../components/Home')
const Welcome = () => import('../components/Welcome')

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path:'/welcome', component:Welcome }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  else {
    const tokenStr = window.sessionStorage.getItem('token')
    // 如果没有token,自动跳转到登录页
    if (!tokenStr) return next('/login')
    next()
  }
})

export default router
